#include "game.h"

Game* Game::instance = new Game;

Game::Game()
{
	modelManager = new ModelManager;
	textureManager = new TextureManager;
	camera = new Camera;
}

Game* Game::getInstance()
{
	return instance;
}

ModelManager* Game::getModelManager()
{
	return modelManager;
}

TextureManager* Game::getTextureManager()
{
	return textureManager;
}

void Game::init()
{
	Block::init();
}

void Game::loop(Drawable * draw)
{
	SDL_Event event;
	Uint32 last_time = SDL_GetTicks();
    Uint32 current_time,ellapsed_time;
    Uint32 start_time;
	
    for (;;)
    {
        start_time = SDL_GetTicks();
        while (SDL_PollEvent(&event))
        {
			
            switch(event.type)
            {
                case SDL_QUIT:
					exit(0);
					break;
					
				case SDL_KEYDOWN:
					camera->keyDown(event.key.keysym.sym);
					break;
					
				case SDL_KEYUP:
					camera->keyUp(event.key.keysym.sym);
					break;
            }
        }
		
        current_time = SDL_GetTicks();
        ellapsed_time = current_time - last_time;
        last_time = current_time;
		
		camera->animate(ellapsed_time);
		
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
		glMatrixMode( GL_MODELVIEW );
		glLoadIdentity( );
		camera->look();
		draw->draw();
		
		glFlush();
		SDL_GL_SwapBuffers();
		
		
        ellapsed_time = SDL_GetTicks() - start_time;
        if (ellapsed_time < 10)
        {
            SDL_Delay(10 - ellapsed_time);
        }
		
    }
}